﻿using Rainbow.common;
using Rainbow.core;
using Rainbow.model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Rainbow.view
{
    /// <summary>
    /// AddConnectDlg.xaml 的交互逻辑
    /// </summary>
    public partial class AddConnectDlg : Window
    {
        public enum OPEnum
        {
            NEW = 0,
            UPDATE,
            COPY
        }

        private OPEnum opFlag { get; set; }

        ConnectInfoViewModel oldInfo;

        public AddConnectDlgViewModel windowViewModel;

        ConnectInfoCollection connectInfoCollection;

        public AddConnectDlg()
        {
            InitializeComponent();

            connectInfoCollection = TW_App.connectInfos;

            windowViewModel = new AddConnectDlgViewModel();
            this.DataContext = windowViewModel;

            this.Loaded += new RoutedEventHandler(Window_Loaded);
        }

        public AddConnectDlg(OPEnum opFlag, ConnectInfoViewModel connectInfoViewModel):this()
        {
            if(connectInfoViewModel != null)
            {
                oldInfo = new ConnectInfoViewModel(connectInfoViewModel);
            }
            else
            {
                oldInfo = new ConnectInfoViewModel();
            }

            this.opFlag = opFlag;
            if(opFlag == OPEnum.NEW)
            {
                windowViewModel.title = "新增设备";
            }
            else if (opFlag == OPEnum.UPDATE)
            {
                windowViewModel.title = "修改设备";
            }
            else if(opFlag == OPEnum.COPY)
            {
                windowViewModel.title = "新增设备";
            }

            windowViewModel.info = new ConnectInfoViewModel(oldInfo);

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.btnOK.Click += new RoutedEventHandler(OK_Click);
            this.btnClose.Click += new RoutedEventHandler(Close_Click);
            
        }

        private void Close_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void OK_Click(object sender, RoutedEventArgs e)
        {
            bool rn = InputCheck();
            if (rn == false) return;

            bool sucessFlag = true;
            if ((opFlag == OPEnum.UPDATE && oldInfo.name != windowViewModel.info.name)
                || opFlag == OPEnum.NEW || opFlag == OPEnum.COPY)
            {
                sucessFlag = SameNameCheck(windowViewModel.info.name);
                if(!sucessFlag)
                {
                    StatusBar.print(MessageType.Error, "名称重复");
                    MessageBox.Show("名称重复","检查",MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }

            if(sucessFlag)
            {
                this.DialogResult = true;
            }
        }

        private bool SameNameCheck(string name)
        {
            int counts = TW_App.connectInfos.Where(x=>x.name == name).Count();
            return counts > 0 ? false : true;
        }

        private bool InputCheck()
        {
            BindingExpression beName = tbName.GetBindingExpression(TextBox.TextProperty);
            beName.UpdateSource();

            BindingExpression beHost = tbHost.GetBindingExpression(TextBox.TextProperty);
            beHost.UpdateSource();

            BindingExpression bePort = tbPort.GetBindingExpression(TextBox.TextProperty);
            bePort.UpdateSource();

            BindingExpression beUsername = tbUsername.GetBindingExpression(TextBox.TextProperty);
            beUsername.UpdateSource();

            //BindingExpression bePass = tbPassPhrase.GetBindingExpression(TextBox.TextProperty);
            //bePass.UpdateSource();

            bool hasError = (Validation.GetHasError(tbName)
                || Validation.GetHasError(tbHost)
                || Validation.GetHasError(tbPort)
                || Validation.GetHasError(tbUsername));

            return !hasError;
        }
    }

    public class AddConnectDlgViewModel : ViewModelBase
    {
        public AddConnectDlgViewModel()
        {
            info = new ConnectInfoViewModel();
        }

        private string m_title;
        public string title
        {
            get { return m_title; }
            set
            {
                m_title = value;
                NotifyPropertyChanged();
            }
        }

        private ConnectInfoViewModel m_info;
        public ConnectInfoViewModel info
        {
            get { return m_info; }
            set
            {
                m_info = value;
                NotifyPropertyChanged();
            }
        }
    }
}
